/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.listener;

import com.legendshop.dao.sql.ConfigCode;
import com.legendshop.oa.handler.impl.ContextServiceLocator;
import com.legendshop.util.AppUtils;
import com.legendshop.util.SystemUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * LegendShop 版权所有 2009-2011,并保留所有权利。
 * 
 * 官方网站：http://www.legendesign.net
 *
 */
public class InitSysListener implements ServletContextListener {

	/** The log. */
	private static Logger log = LoggerFactory.getLogger(InitSysListener.class);

	/** The systemInited. */
	private static boolean systemInited = false;

	private final static Object systemInitedMonitor = new Object();

	/**
	 * 系统初始化.
	 * 
	 * @param event
	 *            the event
	 */
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		log.debug("system destory start");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
	 * .ServletContextEvent)
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		synchronized (systemInitedMonitor) {
			log.info("********* Initializing LegendShop System now *************");
			initSystem(event);
			if (!systemInited) {
				systemInited = true;
				// 2.Spring
				initspring(event);

				// set ConfigCode
				ConfigCode sQlCode = ConfigCode.getInstance();
				//sQlCode.setDebug(PropertiesUtil.isSystemInDebugMode());
				sQlCode.setDebug(true);

				printBeanFactory(ContextServiceLocator.getInstance().getContext());
			}
			log.info("*********  LegendShop System Initialized successful **********");
		}
	}

	private void initSystem(ServletContextEvent event) {

		// 1. 初始化系统真实路径
		String realPath = event.getServletContext().getRealPath("/");
		if (realPath != null && !realPath.endsWith("/") && !realPath.endsWith("\\")) {
			realPath = realPath + "/";
		}
		SystemUtil.setSystemRealPath(realPath);

		ServletContext servletContext = event.getServletContext();
		// 相对路径
		String contextPath = servletContext.getContextPath();
		SystemUtil.setContextPath(contextPath);

	}

	/**
	 * 从Web上下文得到Spring,初始化spring.
	 * 
	 * @param event
	 *            the event
	 * @return true, if successful
	 */
	private void initspring(ServletContextEvent event) {
		ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
		ContextServiceLocator.getInstance().setContext(ctx);
	}

	/**
	 * 打印spring context
	 * 
	 * @param ctx
	 */
	private void printBeanFactory(ApplicationContext ctx) {
		if (log.isDebugEnabled()) {
			int i = 0;
			String[] beans = ctx.getBeanDefinitionNames();
			StringBuffer sb = new StringBuffer("系统配置的Spring Bean [ \n");
			if (AppUtils.isNotBlank(beans)) {
				for (String bean : beans) {
					sb.append(++i).append(" ").append(bean).append("\n");
				}
				sb.append(" ]");
				log.debug(sb.toString());
			}
		}

	}


}
